home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / glx_extensions / video_sync.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  6.3 KB  |  295 lines

  1. /*
  2.  * Copyright 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /* video_sync.c - demonstrate video_sync extension
  18.  * 
  19.  *    Up Arrow     - increment divisor
  20.  *    Down Arrow     - decrement divisor
  21.  *    Left Arrow     - decrement remainder
  22.  *    Right Arrow     - increment remainder
  23.  *    Escape key    - exit program
  24.  */
  25. /* compile: cc -o video_sync video_sync.c -lGLU -lGL -lX11 */
  26.  
  27. #include <GL/glx.h>
  28. #include <GL/glu.h>
  29. #include <X11/keysym.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <math.h>
  33.  
  34. /*  Function Prototypes  */
  35.  
  36. GLvoid  initgfx( GLvoid );
  37. GLvoid  drawScene( GLvoid );
  38. GLvoid    incDivisor( GLvoid );
  39. GLvoid    decDivisor( GLvoid );
  40. GLvoid    incRemainder( GLvoid );
  41. GLvoid    decRemainder( GLvoid );
  42.  
  43. static void printHelp( char * );
  44. static void renderBitmapString( GLuint, char * );
  45. static GLuint LoadFont(Display *dpy, char *fontName);
  46.  
  47. static char *fontName = "-*-helvetica-*-r-normal-*-14-*-*-*-*-*-*-*";
  48. static GLuint font;
  49.  
  50. static GLfloat angle = 0.0;
  51. static int divisor = 20, remainder = 0;
  52.  
  53. static Bool
  54. process_input( Display *dpy ) 
  55. {
  56.     XEvent event;
  57.     static GLboolean animate = GL_TRUE;
  58.     Bool redraw = 0;
  59.     GLsizei w, h;
  60.  
  61.     while (XPending(dpy)) {
  62.         char buf[31];
  63.         KeySym keysym;
  64.  
  65.         XNextEvent(dpy, &event);
  66.         switch(event.type) {
  67.         case Expose:
  68.             redraw = 1;
  69.             break;
  70.         case ConfigureNotify:
  71.             w = event.xconfigure.width;
  72.             h = event.xconfigure.height;
  73.             glViewport(0, 0, w, h);
  74.             redraw = 1;
  75.             break;
  76.         case MapNotify:
  77.             animate = GL_TRUE; 
  78.             break;
  79.         case UnmapNotify:
  80.             animate = GL_FALSE; 
  81.             break;
  82.         case KeyPress:
  83.             (void) XLookupString(&event.xkey, buf, sizeof(buf), 
  84.                         &keysym, NULL);
  85.             switch (keysym) {
  86.             case XK_Up:    /* increment */
  87.                 incDivisor();
  88.                 break;
  89.             case XK_Down:
  90.                 decDivisor();
  91.                 break;
  92.             case XK_Right:
  93.                 incRemainder();
  94.                 break;
  95.             case XK_Left:
  96.                 decRemainder();
  97.                 break;
  98.             case XK_Escape:
  99.                 exit(EXIT_SUCCESS);
  100.                 break;
  101.             default:
  102.                 break;
  103.             }
  104.         default:
  105.             break;
  106.         }
  107.     }
  108.     return redraw || animate;
  109. }
  110.  
  111. int
  112. main(int argc, char **argv) 
  113. {
  114.     int attributeList[] = { GLX_DOUBLEBUFFER, 
  115.                 GLX_RGBA, GLX_RED_SIZE, 1, None };
  116.     Display     *dpy;
  117.     XVisualInfo     *vi;
  118.     GLXContext     cx;
  119.     Bool         isdirect;
  120.     XSetWindowAttributes swa;
  121.     Window         win;
  122.     const char    *s = NULL;
  123.     int         major, minor;
  124.  
  125.  
  126.     dpy = XOpenDisplay(0);
  127.  
  128.     if (!(vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList))) {
  129.         fprintf(stderr, "overlay: no suitable RGB visual available\n");
  130.         exit(EXIT_FAILURE);
  131.     }
  132.  
  133.     /* create a GLX context */
  134.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  135.     isdirect = glXIsDirect(dpy, cx);
  136.  
  137.     swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
  138.         vi->visual, AllocNone);
  139.  
  140.     swa.border_pixel = 0;
  141.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask;
  142.     win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 500, 500,
  143.         0, vi->depth, InputOutput, vi->visual,
  144.         CWBorderPixel|CWColormap|CWEventMask, &swa);
  145.     XStoreName(dpy, win, argv[0]);
  146.     XMapWindow(dpy, win);
  147.  
  148.     glXMakeCurrent(dpy, win, cx);
  149.  
  150.     font = LoadFont( dpy, fontName );
  151.  
  152. #ifdef GLX_SGI_video_sync
  153.     if ( glXQueryVersion(dpy, &major, &minor) == GL_FALSE ) {
  154.         fprintf(stderr, "glXQueryVersion() failed.\n");
  155.         exit(-1);
  156.     }
  157. #ifdef GLX_VERSION_1_1
  158.     if ( minor > 0 || major > 1 )
  159.         s = glXQueryExtensionsString( dpy, DefaultScreen(dpy) );
  160. #endif
  161. #endif
  162.     if (!s || !strstr(s, "GLX_SGI_video_sync")) {
  163.         fprintf(stderr, "GLX_SGI_video_sync not supported\n");
  164.     }
  165.  
  166.     initgfx();
  167.  
  168.     printHelp( argv[0] );
  169.  
  170.     while (1) {
  171.         if (process_input(dpy)) {
  172.             drawScene();
  173.             glXSwapBuffers(dpy, win);
  174.             if (!isdirect) glFinish();
  175.         }
  176.     }
  177. }
  178.  
  179. GLvoid
  180. printHelp( char *progname )
  181. {
  182.     fprintf(stdout, "\n%s - demonstrates video_sync extension\n"
  183.         "Escape key    - exit the program\n"
  184.         "Up Arrow     - increment divisor\n" 
  185.         "Down Arrow     - decrement divisor\n" 
  186.         "Right Arrow     - increment remainder\n" 
  187.         "Left Arrow     - decrement remainder\n\n", 
  188.         progname);
  189. }
  190.  
  191. static GLuint 
  192. LoadFont(Display *dpy, char *fontName)
  193. {
  194.     XFontStruct *fontInfo;
  195.     unsigned long first, last;
  196.     GLuint fontBase;
  197.  
  198.     fontInfo = XLoadQueryFont(dpy, fontName);
  199.     if (fontInfo == NULL) {
  200.         fprintf(stderr, "Can't find font \"%s\"\n", fontName);
  201.         return;
  202.     }
  203.  
  204.     first = fontInfo->min_char_or_byte2;
  205.     last = fontInfo->max_char_or_byte2;
  206.  
  207.     fontBase = glGenLists(last+1);
  208.     if (fontBase == 0) {
  209.         fprintf(stderr, "Out of list space\n");
  210.         return;
  211.     }
  212.     glXUseXFont(fontInfo->fid, first, last-first+1, fontBase+first);
  213.  
  214.     return( fontBase );
  215. }
  216.  
  217. GLvoid
  218. renderBitmapString( GLuint fontBase, char *string )
  219. {
  220.     glPushAttrib( GL_LIST_BIT );
  221.         glListBase( fontBase );
  222.         glCallLists( strlen(string), GL_UNSIGNED_BYTE, string );
  223.     glPopAttrib();
  224. }
  225.         
  226.  
  227. GLvoid
  228. initgfx( GLvoid )
  229. {
  230.     glClearColor( 0.5, 0.5, 0.5, 1.0 );
  231.  
  232.     glMatrixMode( GL_PROJECTION );
  233.     glLoadIdentity();
  234.     glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
  235.     glMatrixMode( GL_MODELVIEW );
  236.     glLoadIdentity();
  237. }
  238.  
  239. GLvoid
  240. incDivisor( GLvoid )
  241. {
  242.     divisor++;
  243. }
  244.  
  245. GLvoid
  246. decDivisor( GLvoid )
  247. {
  248.     if ( divisor > (remainder+1) ) { divisor--; }
  249. }
  250.  
  251.  
  252. GLvoid
  253. incRemainder( GLvoid )
  254. {
  255.     if ( remainder < (divisor-1) ) { remainder++; }
  256. }
  257.  
  258. GLvoid
  259. decRemainder( GLvoid )
  260. {
  261.     if ( remainder > 0 ) { remainder--; }
  262. }
  263.  
  264. GLvoid
  265. drawScene( GLvoid )
  266. {
  267.     char buf[20];
  268.     unsigned int count = 0;
  269.  
  270.     glClear( GL_COLOR_BUFFER_BIT );
  271.  
  272. #ifdef GLX_SGI_video_sync
  273.     glXWaitVideoSyncSGI( divisor, remainder, &count );
  274. #endif
  275.  
  276.     glPushMatrix();
  277.         glRotatef( angle, 0.0, 0.0, -1.0 );
  278.         glColor3f( 1.0, 1.0, 0.0 );
  279.         glBegin( GL_TRIANGLES );
  280.             glVertex2f( -0.1, -0.1 );
  281.             glVertex2f(  0.1, -0.1 );
  282.             glVertex2f(  0.0,  0.9 );
  283.         glEnd();
  284.     glPopMatrix();
  285.  
  286.     /* print stat info */
  287.     glColor3f( 0.0, 0.0, 0.0 );
  288.     sprintf( buf, "divisor: %3d  remainder: %3d count: %3d", 
  289.         divisor, remainder, count );
  290.     glRasterPos2f( -0.5, -0.5 );
  291.     renderBitmapString( font, buf );
  292.  
  293.     angle = fmodf( (angle + 6.0), 360.0 );
  294. }
  295.